package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.Mh1Adapter;
import com.shuorikeji.huohuo.adapter.MhYjFlAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.Mh1FlMainBean;
import com.shuorikeji.huohuo.bean.Mh1FlSubBean;
import com.shuorikeji.huohuo.bean.MhInfoBean;
import com.shuorikeji.huohuo.bean.MhInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.SearchActivity;
import com.shuorikeji.huohuo.ui.mh.MhDetailActivity;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhTypeActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlView;
    @BindView(R.id.rv_mh)
    RecyclerView rvMh;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.rv_yj_fl)
    RecyclerView rvYjFl;
    @BindView(R.id.rv_ej_fl)
    RecyclerView rvEjFl;
    @BindView(R.id.ll_zt_all_tab)
    LinearLayout llZtAllTab;
    @BindView(R.id.tv_zt_all_tab)
    TextView tvZtAllTab;
    @BindView(R.id.iv_zt_all_tab)
    ImageView ivZtAllTab;
    @BindView(R.id.ll_wj_tab)
    LinearLayout llWjTab;
    @BindView(R.id.tv_wj_tab)
    TextView tvWjTab;
    @BindView(R.id.iv_wj_tab)
    ImageView ivWjTab;
    @BindView(R.id.ll_lz_tab)
    LinearLayout llLzTab;
    @BindView(R.id.tv_lz_tab)
    TextView tvLzTab;
    @BindView(R.id.iv_lz_tab)
    ImageView ivLzTab;
    @BindView(R.id.ll_rq_tab)
    LinearLayout llRqTab;
    @BindView(R.id.tv_rq_tab)
    TextView tvRqTab;
    @BindView(R.id.iv_rq_tab)
    ImageView ivRqTab;
    @BindView(R.id.ll_pf_tab)
    LinearLayout llPfTab;
    @BindView(R.id.tv_pf_tab)
    TextView tvPfTab;
    @BindView(R.id.iv_pf_tab)
    ImageView ivPfTab;
    @BindView(R.id.ll_new_tab)
    LinearLayout llNewTab;
    @BindView(R.id.tv_new_tab)
    TextView tvNewTab;
    @BindView(R.id.iv_new_tab)
    ImageView ivNewTab;

    private List<Mh1FlSubBean> mh1FlList = new ArrayList<>();//漫画一级分类数据
    private int mh1FlId;//漫画一级分类id
    private int mh2FlId;//漫画二级分类id
    private MhYjFlAdapter mhYjFlAdapter;
    private List<Mh1FlSubBean> mh2FlList = new ArrayList<>();//漫画二级分类数据
    private MhYjFlAdapter mhEjFlAdapter;
    private List<MhInfoBean> mhList = new ArrayList<>();
    private Mh1Adapter mh1Adapter;
    private int curPage;
    private int mhZt;
    private int mhRq;
    private int mhPf;
    private int mhNews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_type);
    }

    @Override
    protected void initData() {
        mh1FlId = -1;
        initSmartRefreshLayout();
        initAdapter();
        setZtTab(1);
        setSxTab(1);
        getMh1Fl();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlView.setRefreshHeader(new ClassicsHeader(MhTypeActivity.this));
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getAllMh();
            }
        });
        srlView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getAllMh();
            }
        });
    }

    private void initAdapter() {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(this);
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        rvYjFl.addItemDecoration(new GridDividerItemDecoration(20));
        rvYjFl.setLayoutManager(manager);
        mhYjFlAdapter = new MhYjFlAdapter(MhTypeActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlSubBean data = (Mh1FlSubBean) object;
                        for (int i = 0; i < mh1FlList.size(); i++) {
                            if (data.getId() == mh1FlList.get(i).getId()) {
                                mh1FlList.get(i).setSec(true);
                            } else {
                                mh1FlList.get(i).setSec(false);
                            }
                        }
                        mhYjFlAdapter.setData(mh1FlList);
                        mh1FlId = data.getId();
                        getMh2Fl();
                        break;
                }
            }
        });
        rvYjFl.setAdapter(mhYjFlAdapter);

        FlexboxLayoutManager manager1 = new FlexboxLayoutManager(this);
        manager1.setFlexDirection(FlexDirection.ROW);
        manager1.setFlexWrap(FlexWrap.WRAP);
        rvEjFl.addItemDecoration(new GridDividerItemDecoration(20));
        rvEjFl.setLayoutManager(manager1);
        mhEjFlAdapter = new MhYjFlAdapter(MhTypeActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlSubBean data = (Mh1FlSubBean) object;
                        for (int i = 0; i < mh2FlList.size(); i++) {
                            if (data.getId() == mh2FlList.get(i).getId()) {
                                mh2FlList.get(i).setSec(true);
                            } else {
                                mh2FlList.get(i).setSec(false);
                            }
                        }
                        mhEjFlAdapter.setData(mh2FlList);
                        mh2FlId = data.getId();
                        curPage = 1;
                        getAllMh();
                        break;
                }
            }
        });
        rvEjFl.setAdapter(mhEjFlAdapter);

        rvMh.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        rvMh.addItemDecoration(new GridDividerItemDecoration(60));
        mh1Adapter = new Mh1Adapter(MhTypeActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhInfoBean data = (MhInfoBean) object;
                        Intent intent = new Intent(MhTypeActivity.this, MhDetailActivity.class);
                        intent.putExtra("MH_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvMh.setAdapter(mh1Adapter);
    }

    /**
     * @param flag 1人气
     *             2评分
     *             3更新
     */
    private void setSxTab(int flag) {
        tvRqTab.setTextColor((flag == 1) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvRqTab, (flag == 1));
        ivRqTab.setVisibility((flag == 1) ? View.VISIBLE : View.INVISIBLE);
        if (flag == 1) {
            mhRq = 1;
        } else {
            mhRq = 0;
        }

        tvPfTab.setTextColor((flag == 2) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvPfTab, (flag == 2));
        ivPfTab.setVisibility((flag == 2) ? View.VISIBLE : View.INVISIBLE);
        if (flag == 2) {
            mhPf = 1;
        } else {
            mhPf = 0;
        }

        tvNewTab.setTextColor((flag == 3) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvNewTab, (flag == 3));
        ivNewTab.setVisibility((flag == 3) ? View.VISIBLE : View.INVISIBLE);
        if (flag == 3) {
            mhNews = 1;
        } else {
            mhNews = 2;
        }
    }

    /**
     * @param flag 1全部
     *             2完结
     *             3连载
     */
    private void setZtTab(int flag) {
        if (flag == 1) {
            mhZt = -1;
        } else if (flag == 2) {
            mhZt = 2;
        } else if (flag == 3) {
            mhZt = 1;
        }

        tvZtAllTab.setTextColor((flag == 1) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvZtAllTab, (flag == 1));
        ivZtAllTab.setVisibility((flag == 1) ? View.VISIBLE : View.INVISIBLE);

        tvWjTab.setTextColor((flag == 2) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvWjTab, (flag == 2));
        ivWjTab.setVisibility((flag == 2) ? View.VISIBLE : View.INVISIBLE);

        tvLzTab.setTextColor((flag == 3) ? getResources().getColor(R.color.color_FF9B00) : getResources().getColor(R.color.color_333333));
        BusinessUtils.setTxtFold(tvLzTab, (flag == 3));
        ivLzTab.setVisibility((flag == 3) ? View.VISIBLE : View.INVISIBLE);
    }

    private void getMh1Fl() {
        mPresenter.getMh1Fl(false, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlMainBean data = (Mh1FlMainBean) object;
                        mh1FlList.clear();

                        Mh1FlSubBean allBean = new Mh1FlSubBean();
                        allBean.setId(-1);
                        allBean.setName("全部");
                        mh1FlList.add(allBean);
                        allBean = null;

                        mh1FlList.addAll(data.getData());
                        if (mh1FlList != null && mh1FlList.size() > 0) {
                            for (int i = 0; i < mh1FlList.size(); i++) {
                                mh1FlList.get(i).setSec((i == 0) ? true : false);
                            }
                            mh1FlId = mh1FlList.get(0).getId();
                        }
                        mhYjFlAdapter.setData(mh1FlList);
                        getMh2Fl();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhTypeActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void getMh2Fl() {
        mPresenter.mhEjFl(false, mh1FlId, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        Mh1FlMainBean data = (Mh1FlMainBean) object;
                        mh2FlList.clear();

                        Mh1FlSubBean allBean = new Mh1FlSubBean();
                        allBean.setId(-1);
                        allBean.setName("全部");
                        mh2FlList.add(allBean);
                        allBean = null;

                        mh2FlList.addAll(data.getData());
                        if (mh2FlList != null && mh2FlList.size() > 0) {
                            for (int i = 0; i < mh2FlList.size(); i++) {
                                mh2FlList.get(i).setSec((i == 0) ? true : false);
                            }
                            mh2FlId = mh2FlList.get(0).getId();
                            curPage = 1;
                            getAllMh();
                        }
                        mhEjFlAdapter.setData(mh2FlList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhTypeActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 获取所有漫画
     */
    private void getAllMh() {
        mPresenter.getAllMh(6, mhNews, 20, curPage, "", "", mh1FlId, mh2FlId, mhRq, -1,
                mhZt, -1, -1, mhPf, -1, -1, true, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        srlView.finishRefresh();
                        srlView.finishLoadMore();
                        switch (flag) {
                            case 1:
                                MhInfoMainBean data = (MhInfoMainBean) object;
                                if (curPage == 1) {
                                    mhList.clear();
                                }
                                mhList.addAll(data.getData().getData());
                                mh1Adapter.setData(mhList);
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(MhTypeActivity.this, msg);
                                break;
                        }
                    }
                });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        ivSearch.setOnClickListener(view -> {
            Intent intent = new Intent(MhTypeActivity.this, SearchActivity.class);
            startActivity(intent);
        });
        llZtAllTab.setOnClickListener(view -> {
            setZtTab(1);
            curPage = 1;
            getAllMh();
        });
        llWjTab.setOnClickListener(view -> {
            setZtTab(2);
            curPage = 1;
            getAllMh();
        });
        llLzTab.setOnClickListener(view -> {
            setZtTab(3);
            curPage = 1;
            getAllMh();
        });
        llRqTab.setOnClickListener(view -> {
            setSxTab(1);
            curPage = 1;
            getAllMh();
        });
        llPfTab.setOnClickListener(view -> {
            setSxTab(2);
            curPage = 1;
            getAllMh();
        });
        llNewTab.setOnClickListener(view -> {
            setSxTab(3);
            curPage = 1;
            getAllMh();
        });
    }
}